﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zebra.DataAccess;
using Zebra.WinFramework;
using ComponentFactory.Krypton.Toolkit;

namespace Zebra
{
    public partial class UserForm : BaseCRUDForm
    {
        User user;
        public List<User> Users { get; set;}
        BaseGridView gv;

        public UserForm()
        {
            InitializeComponent();
            Initialize();
        }

        #region Methods
        private void Initialize()
        {
            user = User.Create();
            Users = Repository.UserRespository(false).GetAllUsers();
            gv = BaseGridviewSearch.BaseGridview;

            gv.Columns = new string[2] { "Name", "Password" }; //Add columns to gridview  // IS there a better way?
            foreach (string s in gv.Columns)
                gv.gvMain.Columns.Add(gv.AddColumns(s));

            UserBindingSource.DataSource = Users;
            gv.gvMain.DataSource = UserBindingSource;
        }
        private void saveValueToObject()
        {
            user.Name = txtName.Text;
            user.Address = txtAddress.Text;
            user.Dob = DateTime.Now;

            Staff staff = new Staff();
            staff.Password = txtPassword.Text;
            staff.DateJoined = DateTime.Now;
            staff.DateLeft = DateTime.Now;

            user.Staffs.Add(staff);
        }
        private void Clear()
        {
            user = User.Create();
            Utility.Util.ClearTextBoxes(this);

            gv.gvMain.ClearSelection(); //TODO park this to usercontrol
            txtName.Select();
        }
        #endregion

        #region CRUD
        private void BaseCRUDForm_SaveClick(object sender, EventArgs e)
        {
            saveValueToObject();
            if (user.Id == 0)
            {
                Repository.UserRespository(false).Insert(user);
                Users.Add(user);

                UserBindingSource.ResetBindings(false);
                Clear();
            }
            else
            {
                Repository.UserRespository(false).Save();
                UserBindingSource.ResetBindings(false);
            }
        }
        private void BaseCRUDForm_DeleteClick(object sender, EventArgs e)
        {
            if (user != null)
            {
                Users.Remove(user);
                Repository.UserRespository(false).Delete(user);

                UserBindingSource.ResetBindings(false);
            }
        }
        private void BaseCRUDForm_NewClick(object sender, EventArgs e)
        {
            Clear();
        }
        private void BaseCRUDForm_ResetClick(object sender, EventArgs e)
        {
            Repository.UserRespository(false).Refresh(user);
        }
        #endregion

        private void BaseGridviewSearch_DataRowSelected(object sender, DataGridViewCellEventArgs e)
        {
            user = (User)sender;

            if (user == null) return;
            txtName.Text = user.Name;
            txtAddress.Text = user.Address;
            txtPassword.Text = user.Staffs[0].Password;
        }

        private void BaseGridviewSearch_SearchClick(object sender, EventArgs e)
        {
        }

        private void BaseGridviewSearch_TextChangedSearch(object sender, EventArgs e)
        {
            //UserBindingSource.Filter = "Name like '" + sender.ToString() + "%'";  //TODO Why is this bindingSource Filter not working?


            //TODO this event should park under user control.
            if (sender.ToString().Length > 0)
            {
                Users = Users.Where(x => x.Name.StartsWith(sender.ToString())).ToList();
            }
            else
            {
                Users = Repository.UserRespository(false).GetAllUsers();  //TODO this might take up alot of resources.
            }
            UserBindingSource.DataSource = Users;
        }
    }
}
